<?php
class Application_Model_Evento extends Zend_Db_Table_Abstract{
    protected $_name = 'eventos';
    protected $_dependentTables = array("Galeria", "Video");

    protected $_referenceMap = array(
        'Galeria' => array(
            'columns'       => 'galeria_id',
            'refTableClass' => 'Application_Model_Galeria',
            'refColumns'    => 'id'
        ),
        'Video' => array(
            'columns'       => 'video_id',
            'refTableClass' => 'Application_Model_Video',
            'refColumns'    => 'id'
        )
    );

    public function getById($id){
        $select = $this->select()
            ->where("status = 'A' AND id = ?", $id);

        $result = $this->fetchAll($select);
        $arrEvento = $result->toArray();
        return $arrEvento;
    }

    public function getEventos(){
        $select = $this->select()
            ->where(" status = 'A'")
            ->order(array("fecha DESC", "hora DESC"));

        $result = $this->fetchAll($select);
        $arrEventos = $result->toArray();
        return $arrEventos;
    }

    public function getEventosTop3(){
        $select = $this->select()
            ->where(" status = 'A' AND fecha <= Now() AND principal = 1")
            ->order(array("fecha DESC", "hora DESC"))
            ->limit(3);

        $result = $this->fetchAll($select);
        $arrEventos = $result->toArray();
        return $arrEventos;
    }
	
	public function getEventosLimit($limit){
        $select = $this->select()
            ->where(" status = 'A' AND fecha <= Now()")
            ->order(array("fecha DESC", "hora DESC"))
            ->limit($limit);

        $result = $this->fetchAll($select);
        $arrEventos = $result->toArray();
        return $arrEventos;
    }

    public function addEvento($titulo, $hora, $fecha, $lugar, $descripcion, $principal = 0){
        $row = $this->createRow();

        $row->titulo        = $titulo;
        $row->hora          = $hora;
        $row->fecha         = $fecha;
        $row->lugar         = $lugar;
        $row->descripcion   = $descripcion;
        $row->status        = 'A';
        $row->principal     = $principal;

        $row->save();

        $id = $this->_db->lastInsertId();

        return $id;
    }

    public function updateEvento($titulo, $hora, $fecha, $lugar, $descripcion, $principal = 0, $id){

        $row = $this->find($id)->current();

        $row->titulo        = $titulo;
        $row->hora          = $hora;
        $row->fecha         = $fecha;
        $row->lugar         = $lugar;
        $row->descripcion   = $descripcion;
        $row->status        = 'A';
        $row->principal     = $principal;

        if($row->save()){
            return true;
        }else{
            return false;
        }
    }
}